/*
 * File Name：CustomizeApplication.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： CustomizeApplication.java
 * Modify By：res-jxliu
 * Modify Date：2015年3月21日
 * Modify Type：Add
 */
package com.ciwong.mobilelib.application;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Process;
import android.support.multidex.MultiDexApplication;

import com.android.volley.VolleyLog;
import com.ciwong.libs.audio.play.AudioPlayer;
import com.ciwong.libs.jni.CWMedia;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.CWResource;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.libs.utils.DeviceUtils;
import com.ciwong.libs.utils.FileUtils;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.BuildConfig;
import com.ciwong.mobilelib.bean.UserInfoBase;
import com.ciwong.mobilelib.utils.BaseActivityManager;
import com.ciwong.mobilelib.utils.BaseConstants;
import com.ciwong.mobilelib.utils.BaseRequestUtil;
import com.ciwong.mobilelib.utils.BaseSystem;
import com.ciwong.mobilelib.utils.CWUpdate;
import com.ciwong.mobilelib.utils.StatUtils;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class BaseApplication extends MultiDexApplication {

    /**
     * 接口(Html5)环境
     * 1，开发环境；
     * 2，测试环境
     * 3，预发布环境
     * 4，正式环境。
     */
//    public static int RUN_ENV = BuildConfig.RUN_ENV;

    private static final String TAG = "BaseApplication";
    public static int RUN_ENV = 4;
    /**
     * 发布的时候 异常捕获要打开，开发时关闭，为了ide 能直接看到错误日志
     */
    public boolean isRelease = !BuildConfig.DEBUG;
    /**
     * 版本码
     */
    public static boolean sendLog = false;

    private static int APP_ID = 1;

    private String APP_IP = "192.168.0.1";
    private UserInfoBase userInfoBase;
    //运行在手机环境
    public static final int PLATFORM_PHONE = 1001;
    //运行在平板环境
    public static final int PLATFORM_PAD = 1002;
    public static int PLATFORM_ENV = PLATFORM_PHONE;
    /**
     * 当前版本号，最新版本号
     */
    private int curVersionCode, lastVersionCode;

    /**
     * 当前版本是否已升级
     */
    public boolean isVersionUpdate;

    /**
     * 当前版本是否已升级
     */
    private boolean isUpdateCheck = false;

    /**
     * 应用程序标志
     */
    private String appTag;

    /**
     * 是否需要捕捉全局异常
     */
    private boolean isRegistCrashHandler = false;
    private CWCrashHandler crashHandler;
    //护眼模式是否打开
    public boolean isEyeGuardModeOpen;
    public boolean isEyeGuardPerssionGet;

    //音频播放的倍数
    public float mPlayerspeed = 1.0f;

    //字体大小倍数
    public int mFontSizeScale = 1;

    public void setRegistCrashHandler(boolean isRegistCrashHandler) {
        this.isRegistCrashHandler = isRegistCrashHandler;
    }

    public String getAPP_IP() {
        return APP_IP;
    }

    public void setAPP_IP(String APP_IP) {
        this.APP_IP = APP_IP;
    }

    /**
     * 获取appid
     *
     * @return appid
     */
    public static int getAPP_ID() {
        return APP_ID;
    }

    /**
     * 设置appid
     *
     * @param aPP_ID appid
     */
    public static void setAPP_ID(int aPP_ID) {
        APP_ID = aPP_ID;
    }

    public String getAppTag() {
        return appTag;
    }

    public void setAppTag(String appTag) {
        this.appTag = appTag;
    }


    /**
     * 单例模式
     */
    private static BaseApplication instance;

    public static BaseApplication getInstance() {
        if (null == instance) {
            syn();
        }
        return instance;
    }

    public static synchronized void syn() {
        if (null == instance) {
            instance = new BaseApplication();
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setRegistCrashHandler(isRelease);
        VolleyLog.DEBUG = !isRelease;
        if (isRegistCrashHandler) {
            registCrashHandler();
        }
        initAppContext();
        init();
        instance = this;

    }

    /**
     * 注册全局捕获异常
     */
    private void registCrashHandler() {
        crashHandler = CWCrashHandler.getInstance();
        crashHandler.init(this);
    }

    /**
     * 初始化其他
     */
    protected void init() {
        BaseRequestUtil.setContext(getApplicationContext());
    }

    /**
     * 获取用户基础信息
     *
     * @return 用户基础信息
     */
    public UserInfoBase getUserInfoBase() {
        if (userInfoBase == null) {
            try {
                userInfoBase = (UserInfoBase) CWSys.getSharedSerializable(BaseConstants.SHARE_KEY_USER_INFO_BASE);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        StatUtils.userId = this.userInfoBase == null ? 0
                : userInfoBase.getUserId();
        StatUtils.userName = this.userInfoBase == null ? ""
                : userInfoBase.getRealName();
        return userInfoBase;
    }

    /**
     * 设置用户基础信息
     *
     * @param userInfoBase 用户基础信息
     */
    public void setUserInfoBase(UserInfoBase userInfoBase) {
        this.userInfoBase = userInfoBase;
        StatUtils.userId = this.userInfoBase == null ? 0
                : userInfoBase.getUserId();
        StatUtils.userName = this.userInfoBase == null ? ""
                : userInfoBase.getRealName();
    }

    /**
     * 退出
     *
     * @param killProgress 是否杀掉进程
     */
    public void doExit(boolean killProgress) {
        exit(killProgress);
    }

    /**
     * 退出应用
     *
     * @param killProgress 是否结束进程
     */
    public void exit(boolean killProgress) {
        BaseActivityManager.getInstance().killAllActivity();
        if (killProgress) {
            Process.killProcess(Process.myPid());
            // System.exit(0);
        }
    }


    public void setCrashReportListener(CWCrashHandler.CrashReportListener listener) {
        if (null != crashHandler) {
            crashHandler.setCrashReportListener(listener);
        }
    }

    /**
     * 进入主应用才能执行
     */
    public void initAppData() {
        BaseSystem.makeForbidSystemScanFile(getApplicationContext());
        FileUtils.setLogPath(BaseSystem.getProjectFolderPath(getApplicationContext()));
    }

    /**
     *
     */
    public void initAppContext() {
        CWLog.setDebug(!isRelease);
        CWResource.setContext(getApplicationContext());
        CWSys.setContext(getApplicationContext());
        DeviceUtils.setContext(getApplicationContext());
        NetworkUtils.setContext(getApplicationContext());
        AudioPlayer.setContext(getApplicationContext());

    }

    public int gettextZoom() {
        int textZoom = 100;
        if (mFontSizeScale == 0) {
            textZoom = textZoom - 25;
        } else if (mFontSizeScale == 1) {
            textZoom = 100;
        } else if (mFontSizeScale == 2) {
            textZoom = textZoom + 25;
        } else if (mFontSizeScale == 3) {
            textZoom = textZoom + 50;
        } else {
            textZoom = 100;
        }
        return textZoom;
    }
}
